package com.hqyj.suppliers.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.suppliers.dto.SuppliersInfo;
import com.hqyj.suppliers.entity.Suppliers;
import com.hqyj.suppliers.mapper.SuppliersMapper;
import com.hqyj.suppliers.service.SuppliersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.user.pojo.UserRole;
import com.hqyj.utils.PageUtil;
import com.hqyj.utils.ResultUtils;
import com.sun.org.apache.regexp.internal.REUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sdx2009
 * @since 2022-09-30
 */
@Service
public class SuppliersServiceImpl extends ServiceImpl<SuppliersMapper, Suppliers> implements SuppliersService {

    @Override
    public ResultUtils suppliersIndex(SuppliersInfo suppliersInfo) {
        Page<Suppliers> page = new Page<>(suppliersInfo.getPage(),suppliersInfo.getRow());
        QueryWrapper<Suppliers> qw = new QueryWrapper<>();
        qw.like(suppliersInfo.getSuppliersName() != null && !suppliersInfo.getSuppliersName().equals(""),"suppliers_name",suppliersInfo.getSuppliersName());
        qw.like(suppliersInfo.getSuppliersEmail() != null && !suppliersInfo.getSuppliersEmail().equals(""),"suppliers_email",suppliersInfo.getSuppliersEmail());
        qw.like(suppliersInfo.getSuppliersTel() != null && !suppliersInfo.getSuppliersTel().equals(""),"suppliers_tel",suppliersInfo.getSuppliersTel());

        Page<Suppliers> pageList = this.page(page,qw);
        PageUtil<Suppliers> pageUtil = new PageUtil<>();
        pageUtil.setData(pageList.getRecords());
        pageUtil.setTotal(pageList.getTotal());
        pageUtil.setTotalPage(pageList.getPages());
        return new ResultUtils(200,"",pageUtil);
    }

    @Override
    public ResultUtils suppliersUpdate(SuppliersInfo suppliersInfo) {
        UpdateWrapper<Suppliers> updateWrapper = new UpdateWrapper<>();
        Suppliers suppliers = new Suppliers();
        BeanUtils.copyProperties(suppliersInfo,suppliers);
        updateWrapper.eq("suppliers_name",suppliersInfo.getSuppliersName());
        if (this.update(suppliers,updateWrapper)){
            return new ResultUtils(200,"修改成功！",null);
        }
        return new ResultUtils(500,"修改失败！",null);
    }

    @Override
    public ResultUtils deleteSuppliers(SuppliersInfo suppliersInfo) {
        if (this.removeById(suppliersInfo.getSuppliersId())){
            return new ResultUtils(200,"删除成功！",null);
        }
        return new ResultUtils(500,"删除失败！",null);
    }

    @Override
    public ResultUtils addSuppliers(SuppliersInfo suppliersInfo) {
        Suppliers suppliers = new Suppliers();
        BeanUtils.copyProperties(suppliersInfo,suppliers);
        if (this.save(suppliers)){
            return new ResultUtils(200,"添加成功！",null);
        }
        return new ResultUtils(500,"添加成功！",null);
    }

    @Override
    public ResultUtils selectSuppliers() {
        return new ResultUtils(200,"",this.list());
    }
}
